package com.test.auth_db_auto.support.code.sms;

import com.test.auth_db_auto.support.BootSecurityProperties;
import com.test.auth_db_auto.support.DefaultBeanName;
import com.test.auth_db_auto.support.code.BootCodeService;
import com.test.auth_db_auto.support.code.VerificationCode;
import com.test.auth_db_auto.support.code.VerificationCodeGeneration;
import com.test.auth_db_auto.support.properities.BootSmsLoginProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @ Author: .Mr
 * @ ClassName DefaultSmsCodeGenerator
 * @ Description TODO
 * @ date 2022/1/5 11:33
 * @ Version 1.0
 * 自定义手机号验证码发送
 */
@Component(DefaultBeanName.DEFAULT_SMS_CODE_GENERATOR_BEAN)
@Slf4j
public class DefaultSmsCodeGenerator implements VerificationCodeGeneration<String,String> {

   private BootSecurityProperties properties;
   private BootCodeService<String> codeService;

   private final static String CODE_STR="0123456789";

   public DefaultSmsCodeGenerator(BootSecurityProperties properties,BootCodeService<String> codeService){
       this.properties=properties;
       this.codeService=codeService;
   }

    @Override
    public VerificationCode<String> generator(String key) throws Exception {

       VerificationCode<String> vc=new VerificationCode<>();
       String code= RandomStringUtils.random(properties.getSmsLogin().getLength(),CODE_STR);
       vc.setContent(code);
       vc.setExpirationTime(properties.getCodeExpireTime());
       codeService.setCodeValue(key,code);

       log.info("登录验证码:{},{}分钟内有效",vc.getContent(),vc.getExpirationTime()/60);
       return vc;
    }
}
